import os

from Base import GetYamlFile
from Base.ReadExcel import Read_Excel
from Base.WriteExcel import Write_excel

YmlList = GetYamlFile.getYamlFile()
driverPath = YmlList["driverPath"]
driverWritePath = YmlList["driverWritePath"]
carPath = YmlList["carPath"]
carWritePath = YmlList["carWritePath"]
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))


def driver_same_filter():
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, driverPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    name_list = []
    driver_list = []
    for i in range(1, num):
        phone = readBook.read_Excel("sheet", i, 0)
        name = readBook.read_Excel("sheet", i, 1)
        name = name.replace(" ", "")
        phone = phone.replace(" ", "")
        if name in name_list:
            print("已有该姓名司机：{}".format(name))
        else:
            name_list.append(name)
            driver_list.append({"name": name, "phone": phone})
    return driver_list


def driver_write(driver_list):
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, driverWritePath)
    wt = Write_excel(REPORT_XLSX_DIR, "sheet")
    num = 1
    for item in driver_list:
        wt.write(num, 1, item.get("phone"))
        wt.write(num, 2, item.get("name"))
        wt.write(num, 3, item.get("name"))
        print("司机写入：{}".format(num))
        num += 1


def car_same_filter():
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, carPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    car_list = []
    for i in range(1, num):
        car = readBook.read_Excel("sheet", i, 0)
        gb = readBook.read_Excel("sheet", i, 6)
        car = car.replace(" ", "")
        if car in car_list:
            print("已有该车辆：{}".format(car))
        else:
            car_list.append({"car": car, "gb": gb})
    return car_list


def car_write(car_list):
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, carWritePath)
    wt = Write_excel(REPORT_XLSX_DIR, "sheet")
    num = 2
    for item in car_list:
        wt.write(num, 1, item.get("car"))
        wt.write(num, 7, item.get("gb"))
        print("车辆写入：{}".format(num))
        num += 1


if __name__ == '__main__':
    driver_list = driver_same_filter()
    driver_write(driver_list)

    car_list = car_same_filter()

    car_write(car_list)
